package com.blockmeta.bbs.baselibrary.utils;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;

import com.blockmeta.bbs.baselibrary.base.app.BaseApp;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;


/**
 * APP環境的定義
 *
 * @author websocket
 */

public class AppEnv {


    public enum AppLanguage {
        EN("EN", 1, "US", 1000), CN("CN", 2, "CN", 10000), TW("TW", 2, "TW", 10000);

        static private HashMap<String, AppLanguage> m_map = new HashMap<String, AppLanguage>();

        static {
            for (AppLanguage lan : AppLanguage.values()) {
                m_map.put(lan.getCode(), lan);
            }
        }

        protected int m_nBytes; // 該語系 一個文字佔多少Byte
        protected String m_strCode;
        protected String m_strCountryCode; // defined by ISO 3166-1
        protected int m_nFormatUnit;

        /**
         * 是否一個文字 為2Bytyes
         *
         * @param code
         * @param bytes
         * @return
         */

        AppLanguage(String code, int bytes, String countryCode, int formatUnit) {
            m_strCode = code;
            m_nBytes = bytes;
            m_strCountryCode = countryCode;
            m_nFormatUnit = formatUnit;
        }

        public int getUnit() {
            return m_nFormatUnit;
        }

        public static AppLanguage getLanguage(String code) {
            AppLanguage lan = m_map.get(code);
            if (lan == null) {
                lan = AppLanguage.EN;
            }
            return lan;
        }

        public String getCode() {
            return m_strCode;
        }

        public int getBytes() {
            return m_nBytes;
        }

        public boolean isSimpleChinese() {
            return m_strCode.equals("CN");
        }

        public boolean isTraditionChinese() {
            return m_strCode.equals("TW");
        }

        public String getCountryCode() {
            return m_strCountryCode;
        }

        public static Set<String> getCodeSet() {
            AppLanguage[] values = AppLanguage.values();
            Set<String> codes = new HashSet<String>();
            for (AppLanguage language : values) {
                codes.add(language.getCode());
            }
            return codes;
        }
    }


    private static AppLanguage m_AppLanguage;

    public static AppLanguage getLanguage() {
        /**
         * follow 5.0 spec,
         * Global version : show zh_TW if choose hk_TW or zh_TW locale
         *                  shoe EN if choose other locale
         * CN version : show CN words
         */

        if (m_AppLanguage == null) {
            AppLanguage language = AppLanguage.CN;

            try {
                String country = getLocalLanguageCountry();
                if (country.equals("HK") || country.equals("TW")) {
                    language = AppLanguage.TW;
                } else if (country.equals("CN")) {
                    language = AppLanguage.CN;
                } else {
                    language = AppLanguage.EN;
                }
            } catch (Exception e) {
                language = AppLanguage.CN;
            }

            m_AppLanguage = language;
        }

        return m_AppLanguage;
    }


    public static void setLanguage(AppEnv.AppLanguage language) {


        //update language
        m_AppLanguage = language;

//		FDTStorage.getInstance().put(SaveKey.LOCAL_LANGUAGE, language.getCountryCode(), SaveKey.GLOBAL_FILE);

//		MainActivity context = MyApplication.getIns().getMainActivity();

        Resources resources = BaseApp.getApp().getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        Configuration config = new Configuration();
        // 应用用户选择语言


        switch (language) {
            case CN:
                config.locale = Locale.SIMPLIFIED_CHINESE;
                break;

            case TW:
                config.locale = Locale.TRADITIONAL_CHINESE;
                break;

            default:
                config.locale = Locale.ENGLISH;
                break;
        }

        resources.updateConfiguration(config, dm);

//		MyApplication.getIns().createConfigurationContext(config);


//		Intent intent = new Intent(context, MainActivity.class);
//		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//		context.startActivity(intent);

//		// 杀掉进程
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);

    }

    private static String getLocalLanguageCountry() {


        return "CN";
    }


    public static void resetLanguage() {

        Resources resources = BaseApp.getApp().getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        Configuration config = new Configuration();
        // 应用用户选择语言

        config.locale = getSetLocale();


        resources.updateConfiguration(config, dm);
    }


    /**
     * 是否是设置值
     *
     * @return 是否是设置值
     */
    public static boolean isSetValue() {
        Locale currentLocale = BaseApp.getApp().getResources().getConfiguration().locale;
        return currentLocale.equals(getSetLocale());
    }

    public static Locale getSetLocale() {
        switch (getLanguage()) {
            case CN:
                return Locale.SIMPLIFIED_CHINESE;

            case TW:
                return Locale.TRADITIONAL_CHINESE;

            default:
                return Locale.ENGLISH;
        }
    }

}
